<template>
  <el-dropdown trigger="click">
    <div class="avatar">
      <!--      <img src="@/assets/images/avatar.gif" alt="avatar" />-->
      <!--      <el-icon style="position: relative; top: 3px"><User /></el-icon>-->
      <span class="iconfont icon-user" style="position: relative; top: 1px"></span>
      <span class="username">{{ username }}</span>
      <el-icon><ArrowDownBold /></el-icon>
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click="openDialog('infoRef')">
          <el-icon><User /></el-icon>个人信息
        </el-dropdown-item>
        <el-dropdown-item @click="openDialog('passwordRef')">
          <el-icon><Edit /></el-icon>修改密码
        </el-dropdown-item>
        <el-dropdown-item divided @click="logout">
          <el-icon><SwitchButton /></el-icon>退出
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
  <!-- infoDialog -->
  <InfoDialog ref="infoRef"></InfoDialog>
  <!-- passwordDialog -->
  <PasswordDialog ref="passwordRef"></PasswordDialog>
</template>

<script setup lang="ts">
import { computed, ref } from "vue";
import { LOGIN_URL } from "@/config";
// import { useRouter } from "vue-router";
// import { getUserInfo } from "@/api/modules/userManagement";
import { useUserStore } from "@/stores/modules/user";
import { ElMessageBox, ElMessage } from "element-plus";
import InfoDialog from "./InfoDialog.vue";
import PasswordDialog from "./PasswordDialog.vue";

// const router = useRouter();
const userStore = useUserStore();
// let user = reactive({ username: "admin" });
const username = computed(() => userStore.userInfo.display_name);
// const userInfo = () => {
//   const res: any = getUserInfo();
//   if (res.data) {
//     user.username = res.data.username;
//   }
// };
// userInfo();

// 退出登录
const logout = () => {
  ElMessageBox.confirm("您是否确认退出登录?", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(async () => {
    // 1.执行退出登录接口
    // await logoutApi();

    // 2.清除 Token
    userStore.setToken("");
    localStorage.removeItem("token");

    // 3.重定向到登陆页
    // router.replace(LOGIN_URL);
    window.location.replace(LOGIN_URL);
    ElMessage.success("退出登录成功！");
  });
};

// 打开修改密码和个人信息弹窗
const infoRef = ref<InstanceType<typeof InfoDialog> | null>(null);
const passwordRef = ref<InstanceType<typeof PasswordDialog> | null>(null);
const openDialog = (ref: string) => {
  if (ref == "infoRef") infoRef.value?.openDialog();
  if (ref == "passwordRef") passwordRef.value?.openDialog();
};
</script>

<style scoped lang="scss">
.avatar {
  min-width: 40px;
  height: 40px;
  overflow: hidden;
  line-height: 40px;
  cursor: pointer;
  img {
    height: 100%;
  }
  .username {
    display: inline-block;
    margin: 0 6px;
    margin-left: 3px;
    font-size: 14px;
    line-height: 40px;
  }
}
</style>
